package com.airforce.controller;


import com.airforce.config.Redisconfig;
import com.airforce.mappers.servicesMapper;
import com.airforce.model.services;
import com.airforce.utils.JsonUtil;
import com.airforce.utils.ResultCode;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.List;

@RequestMapping("system")
@RestController
public class servicesCon {


    @Autowired
    servicesMapper servicesMapper;

    private  final static Logger logger=LoggerFactory.getLogger(servicesCon.class);


    //查询客服信息
    @GetMapping("servicelist")
    public Object servicelist(
            @RequestParam (required = false)String name,
            @RequestParam(required = false) int index ,
            @RequestParam(required = false) int num
    ){

        int PageTotal=0;

        if(name==null) {


            PageTotal = servicesMapper.selectByall().size();
            PageHelper.startPage(index, num);
            List<services> services = servicesMapper.selectByall();

            return ResultCode.SUCCESS.result(services, PageTotal);
        }else {
            String parmname=java.net.URLEncoder.encode(name);
            PageTotal=servicesMapper.selectByname("%"+parmname+"%").size();
            PageHelper.startPage(index,num);
            List<services>services=servicesMapper.selectByname("%"+parmname+"%");
            return ResultCode.SUCCESS.result(services,PageTotal);
        }
    }

    //删除客服信息
    @GetMapping("serviceDel")
    public Object servicedel(@RequestParam Integer id){

       logger.info("进行删除客服信息"+id);
        servicesMapper.deleteByPrimaryKey(id);


        return ResultCode.SUCCESS.result();
    }


    //更新客服信息
    @PostMapping("serviceUp")
    public Object serviceUp(@RequestBody services services){

       logger.info("开始更新客服数据"+JsonUtil.toJson(services));
        servicesMapper.updateByPrimaryKeySelective(services);
        return ResultCode.SUCCESS.result();
    }

    //更新客服状态
    @PostMapping("serviceisuse")
    public Object serviceisuse(@RequestBody services services){
                byte isuse=servicesMapper.selectByPrimaryKey(services.getId()).getIsuse();
                services parm=new services();
                parm.setId(services.getId());
                if(isuse==1){
                    parm.setIsuse((byte)0);
                }else if(isuse==0){
                    parm.setIsuse((byte)1);
                }
        logger.info("更新客服状态"+services);
                servicesMapper.updateByPrimaryKeySelective(parm);
            return     ResultCode.SUCCESS.result();
    }


    //添加客服数据
    @PostMapping("service")
    public Object servicePost(@RequestBody services services){
    logger.info("添加客服："+services.getName());
        servicesMapper.insertSelective(services);
        return ResultCode.SUCCESS.result();
    }



}
